import Vue from "vue";
import Vuex from "../myVuex";
import products from "./modules/products";
Vue.use(Vuex);

const myPlugin = store => {
    store.subscribe((mutation, state) => {
        console.log(mutation);
        console.log(state);
    });
};

export default new Vuex.Store({
    plugins: [myPlugin],
    strict: process.env.NODE_ENV !== "production", // 严格模式 生产环境不要开启 会影响性能
    state: {
        count: 0,
        msg: "hello word",
    },
    getters: {
        reverseMsg(state) {
            return state.msg
                .split("")
                .reverse()
                .join("");
        },
    },
    mutations: {
        add(state, payload) {
            state.count += payload;
        },
    },
    actions: {
        asyncAdd(context, payload) {
            setTimeout(() => {
                console.log(context);
                context.commit("add", payload);
            }, 2000);
        },
    },
    modules: {
        products,
    },
});